package com.pokeoffice.users;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.pokeoffice.database.Control;
import com.pokeoffice.medical.Treatment;



/**
 * The Class User.
 *
 * @author  Kyle Davis  (Pattern: Template) This class is used to provide a structure for the  different types of users in the system  TODO Everything, Regex
 */
public abstract class User {

	// Server Information
	/**
	 * Gets the iD.
	 *
	 * @return the iD
	 */
	public abstract Long getID();
	
	/**
	 * Sets the iD.
	 *
	 * @param id the new iD
	 */
	public abstract void setID(long id);
	
	/**
	 * Gets the connection.
	 *
	 * @return the connection
	 */
	public abstract Connection getConnection();
	
	/**
	 * Sets the connection.
	 *
	 * @param con the new connection
	 */
	public abstract void setConnection(Connection con);
	
	/**
	 * Gets the user type.
	 *
	 * @return the user type
	 */
	public abstract UserType getUserType();
	
	/**
	 * Sets the user type.
	 *
	 * @param u the new user type
	 */
	public abstract void setUserType(UserType u);

	// Personal Information
	/**
	 * Gets the name.
	 *
	 * @return the name
	 */
	public abstract String getName();
	
	/**
	 * Sets the name.
	 *
	 * @param s the new name
	 */
	public abstract void setName(String s);
	
	/**
	 * Gets the birthdate.
	 *
	 * @return the birthdate
	 */
	public abstract String getBirthdate();
	
	/**
	 * Sets the birthdate.
	 *
	 * @param s the new birthdate
	 */
	public abstract void setBirthdate(String s);
	
	/**
	 * Gets the gender.
	 *
	 * @return the gender
	 */
	public abstract String getGender();
	
	/**
	 * Sets the gender.
	 *
	 * @param s the new gender
	 */
	public abstract void setGender(String s);
	
	/**
	 * Gets the phone.
	 *
	 * @return the phone
	 */
	public abstract String getPhone();
	
	/**
	 * Sets the phone.
	 *
	 * @param s the new phone
	 */
	public abstract void setPhone(String s);
	
	/**
	 * Gets the city.
	 *
	 * @return the city
	 */
	public abstract String getCity();
	
	/**
	 * Sets the city.
	 *
	 * @param s the new city
	 */
	public abstract void setCity(String s);
	
	/**
	 * Gets the region.
	 *
	 * @return the region
	 */
	public abstract String getRegion();
	
	/**
	 * Sets the region.
	 *
	 * @param s the new region
	 */
	public abstract void setRegion(String s);

	// Medical Information
	/**
	 * Gets the pharmacy.
	 *
	 * @return the pharmacy
	 */
	public abstract String getPharmacy();
	
	/**
	 * Sets the pharmacy.
	 *
	 * @param s the new pharmacy
	 */
	public abstract void setPharmacy(String s);
	
	/**
	 * Gets the insurance.
	 *
	 * @return the insurance
	 */
	public abstract String getInsurance();
	
	/**
	 * Sets the insurance.
	 *
	 * @param s the new insurance
	 */
	public abstract void setInsurance(String s);
	
	// Not Pre-loaded
	/**
	 * Record.
	 *
	 * @return the list
	 */
	public abstract List<Treatment> record();	// All
	
	/**
	 * Record.
	 *
	 * @param date the date
	 * @return the list
	 */
	public abstract List<Treatment> record(String date);	// On the specified DAY
	
	/**
	 * Record.
	 *
	 * @param startDate the start date
	 * @param endDate the end date
	 * @return the list
	 */
	public abstract List<Treatment> record(String startDate, String endDate);	// From start to end (inclusive)

	// Non-Abstract Methods
	/**
	 * Gets the login.
	 *
	 * @return the login
	 */
	public String getLogin() {
		return "" + getName().charAt(0) + getID();
	}

	/**
	 * Grants.
	 *
	 * @return the string
	 */
	public String grants() {
		return getUserType().grants();
	}

	/**
	 * Logs out of the system by setting the connection to null.
	 *
	 * @return true for successful logout
	 */
	public boolean logout() {

		try {
			getConnection().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		setConnection(null);
		return true;
	}

	/**
	 * Gets the current login attempts for this user.
	 *
	 * @return the login attempts for this user
	 */
	public int getLoginAttempts() {
		Control ctrl = Control.get();
		int attempts = ctrl.MAX_LOGIN_ATTEMPTS;
		attempts = ctrl.getLoginAttempt(getID());
		return attempts;
	}

	/**
	 * Returns the username of this user.
	 *
	 * @return the username of this user
	 */
	public String username() {
		return getName().charAt(0) + "" + getID();
	}

	/**
	 * Gets the userID of a user with a given username.
	 *
	 * @param username the username of a user
	 * @return the id of a user with the username
	 */
	public static long getID(String username) {
		return Long.parseLong(username.substring(1));
	}

}
